<?php
namespace Common\Model;
use Common\Model\AiModel;
class OrderModel extends AiModel {
    /**
     * @param $examine_state
     * @param $testing_code
     * 设置订单审核状态
     */
    public function setOrder($id,$examine_state=0,$testing_code=''){
        if($examine_state==0) return true;
        $data=array(
            'examine_state' => $examine_state,
            'testing_code'  => $testing_code,
            'examine_time'  => ADD_TIME,
            'examine_user'  => ADMIN_NAME
        );
        $this->where("id={$id}")->data($data)->limit(1)->save();
        #推送消息给医生

        #推送消息给用户

        return true;
    }

    /**
     * @param int $goods_id
     * 添加订单
     */
    public function addOrder($goods_id=0){
        #加入到payLog表
        $openid = $_COOKIE['wxopenid'];
        $user_id = M('UserWechat')->where("openid='{$openid}'")->getField('user_id');
        $money = M('Goods')->where("id={$goods_id}")->getField('price');
        $pay_log_id = D('PayLog')->addPayLog(3,'','hypertension',$money,$user_id);
        $mobile = M('User')->where("id={$user_id}")->getField('mobile');
        $realname = M('User')->where("id={$user_id}")->getField('realname');

        $data = array(
            'good_id'    => $goods_id,
            'price'      => $money,
            'ordertime'  => ADD_TIME,
            'pay_log_id' => $pay_log_id,
            'uid'        => $user_id,
            'doctor_id'  => 0,
            'mobile'     => $mobile,
            'realname'   => $realname,
        );
        $order_id = $this->add($data);
        return array(
            'pay_log_id' => $pay_log_id,
            'price'      => $money,
            '$rder_id'   => $order_id,
        );
    }

    /**
     * @param $pay_log_id
     * @return mixed
     * 获取支付状态
     */
    public function getPayState($pay_log_id)
    {
        $pay_time = $this->where(array('pay_log_id' => $pay_log_id))->getField('paytime');
        return $pay_time;
    }

    /**
     * @param int $pay_log_id
     * 设置支付状态
     */
    public function setPay($pay_log_id=0){
        $data = array(
            'status'  => 1,
            'paytime' => ADD_TIME
        );
        $this->where("pay_log_id={$pay_log_id}")->data($data)->limit(1)->save();
        return true;
    }

    /**
     * @param int $user_id
     * @param int $result
     * 获取用户订单
     */
    public function getUserOrder($user_id=0,$result=1,$type='user'){

        if($type=='user'){
            $where=" uid=$user_id ";
        }else{
            $where=" doctor_id=$user_id ";
        }

        $where.=" and paytime>0 ";

        if($result>1){
            $where.=" and grab_time>0 ";
        }
        $result = $this->where($where)->order("id desc")->select();
        if($result){
            foreach($result as $key=>&$val){
                $val['user_age']  = D('User')->getAge($val['uid']);
                $val['ordertime'] = get_datetime($val['ordertime']);//下单时间
                $val['grab_time'] = get_datetime($val['grab_time']);//获取报告时间
                $val['testing_title']  = '高血压检测项目';//检测内容
                $val['testing_method'] = '口腔粘膜样/飞行质谱测序';//检测方法
            }
        }
        return $result;
    }

    /**
     * @param int $order_id
     * @param string $doctor_tell_content
     * 填写医嘱
     */
    public function sendAdvide($order_id=0,$doctor_tell_content=''){
        $where=" id=$order_id ";
        $data = array(
            'doctor_tell' => $doctor_tell_content
        );
        $this->where($where)->data($data)->save();
        file_put_contents('2.sql',$this->getLastSql());
        return true;
    }
}